package com.example.yimai.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.yimai.entity.ShopCartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ShopCartService {

    @Autowired
    private RedisTemplate<String , Object> redisTemplate;

    private static final String SHOP_CART_KEY_PREFIX = "cart_";

    public String addToCart(Long userId, ShopCartItem shopCartItem){
        try {
            String key = SHOP_CART_KEY_PREFIX + userId;
            String productId = shopCartItem.getProductId();

            String existingItemJson = (String) redisTemplate.opsForHash().get(key, productId);
            if(existingItemJson != null){
                ShopCartItem existingItem = JSON.parseObject(existingItemJson, ShopCartItem.class);
                existingItem.setQuantity(existingItem.getQuantity() + shopCartItem.getQuantity());
                redisTemplate.opsForHash().put(key, productId, JSON.toJSONString(existingItem));
            }else redisTemplate.opsForHash().put(key, shopCartItem.getProductId(), JSON.toJSONString(shopCartItem));
            return "success";
        }catch (Exception e){
            throw new RuntimeException("添加购物车失败"+e.getMessage());
        }
    }

    public List<ShopCartItem> getCart(Long userId){
        try {
            String key = SHOP_CART_KEY_PREFIX + userId;
            Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
            return entries.values().stream().map(item -> JSON.parseObject((String) item, ShopCartItem.class)).toList();
        }catch (Exception e){
            throw new RuntimeException("获取购物车数据失败");
        }
    }

    public String updateQuantity(Long userId, String productId, int quantity){
        try {
            String key = SHOP_CART_KEY_PREFIX + userId;
            String itemJson = (String) redisTemplate.opsForHash().get(key, productId);
            if (itemJson != null) {
                ShopCartItem shopCartItem = JSON.parseObject(itemJson, ShopCartItem.class);
                shopCartItem.setQuantity(quantity);
                redisTemplate.opsForHash().put(key, productId, JSON.toJSONString(shopCartItem));
            }
            return "success";
        }catch (Exception e){
            throw new RuntimeException("更新购物车数量失败");
        }
    }

    public String removeFromCart(Long userId, String productId){
        try {
            String key = SHOP_CART_KEY_PREFIX + userId;
            redisTemplate.opsForHash().delete(key, productId);
            return "success";
        }catch (Exception e){
            throw new RuntimeException("删除购物车商品失败");
        }
    }

    public String clearCart(Long userId){
        try {
            String key = SHOP_CART_KEY_PREFIX + userId;
            redisTemplate.delete(key);
            return "success";
        }catch (Exception e){
            throw new RuntimeException("清空购物车失败");
        }
    }

    public Long getTotalCount(Long userId){
        String key = SHOP_CART_KEY_PREFIX + userId;
        return redisTemplate.opsForHash().size(key);
    }

}
